package io.valuesfeng.picker.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.List;

import io.valuesfeng.picker.PreviewImageActivity;
import io.valuesfeng.picker.R;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by dune on 16/1/11.
 */
public class PreviewImagePaperAdapter extends PagerAdapter {

    private List<String> images;
    private Activity mActivity;
    private OnItemPhotoTapListener mOnItemPhotoTapListener;


    public PreviewImagePaperAdapter(List<String> images, Activity activity) {

        this.images = images;
        mActivity = activity;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(container);
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        RelativeLayout relativeLayout = (RelativeLayout) mActivity.getLayoutInflater().inflate(
                R.layout.item_image_display, null);

        final PhotoView photoView = (PhotoView) relativeLayout.findViewById(R.id.image_display_item_imgv);
        relativeLayout.setTag(photoView);
        Glide.with(mActivity).load(images.get(position))
                .centerCrop()
                .fitCenter()
                .thumbnail(0.8f)
                .crossFade()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        photoView.setImageDrawable(resource);
                        ((PreviewImageActivity) mActivity).setProcessShow(false);
                    }
                });
        photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                if (mOnItemPhotoTapListener != null) {

                    mOnItemPhotoTapListener.onPhotoTap(view, x, y);
                }
            }

            @Override
            public void onOutsidePhotoTap() {

            }
        });

        container.addView(relativeLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        return relativeLayout;
    }

    @Override
    public int getCount() {

        return images.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    public void setOnItemPhotoTapListener(OnItemPhotoTapListener onItemPhotoTapListener) {
        mOnItemPhotoTapListener = onItemPhotoTapListener;
    }

    public interface OnItemPhotoTapListener {

        void onPhotoTap(View view, float x, float y);
    }
}
